package cloud.xlh.service.file.service;

import cloud.xlh.util.SFTPUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created with IntelliJ IDEA.
 *
 * @ project name : xlh_osms
 * @ Author: XuLeHuang
 * @ Date: 2023/8/5 18:16
 * @ Description:
 */
@Service
public class RemoteFileService {
    //ip地址
    @Value("${sftp.host}")
    private String host;
    //登录账号
    @Value("${sftp.username}")
    private String username;
    //登录密码
    @Value("${sftp.password}")
    private String password;
    //默认端口号
    @Value("${sftp.port}")
    private int port;
    //默认过期时间
    @Value("${sftp.timeout}")
    private int timeout;

    private static String defaultMpLocalDirPath;
    private static String defaultMpRemoteDirPath;

    private ConcurrentHashMap<String, String> mountDirMpMap = new ConcurrentHashMap<>();

    static {
        init();
    }

    static void init() {
        defaultMpLocalDirPath = "D:\\";
        defaultMpRemoteDirPath = "/"+"home"+"/"+"xlh";
    }

    public void setDefaultMpLocalDirPath(String defaultMpLocalDirPath) {
       this.defaultMpLocalDirPath = defaultMpLocalDirPath;
    }

    public ConcurrentHashMap<String, String> getMountDirMpMap() {
        return mountDirMpMap;
    }

    @Async
    public void asyncPut(String src) {
        String remoteDir = src.substring(0 , src.lastIndexOf(File.separator));
        String remoteFileName = src.substring(src.lastIndexOf(File.separator) + 1);
        String newDst = mountDirMpMap
                .containsKey(remoteDir)
                ? new StringBuilder(mountDirMpMap.get(remoteDir))
                .append("/").append(remoteFileName).toString()
                : new StringBuilder(defaultMpRemoteDirPath).append("/").append(remoteFileName).toString();
        SFTPUtil sftpUtil = new SFTPUtil(host, username, password, port, timeout);
        sftpUtil.put(src , newDst);
    }
    
    @Async
    public void asyncPut(String src , String dst) {
        SFTPUtil sftpUtil = new SFTPUtil(host, username, password, port, timeout);
        sftpUtil.put(src , dst);
    }

    @Async
    public void asyncRemove(String dst) {
        SFTPUtil sftpUtil = new SFTPUtil(host, username, password, port, timeout);
        sftpUtil.remove(dst);
    }

    public String downFile(String src, String dst) {
        String localDir = dst.substring(0 , src.lastIndexOf(File.separator));
        String localFileName = src.substring(src.lastIndexOf(File.separator) + 1);
        String newDst = mountDirMpMap
                .containsKey(localDir)
                ? new StringBuilder(localDir)
                .append("\\").append(localFileName).toString()
                : new StringBuilder(defaultMpLocalDirPath).append("\\").append(localFileName).toString();
        SFTPUtil sftpUtil = new SFTPUtil(host, username, password, port, timeout);
        sftpUtil.down(src , newDst);
        return newDst;
    }
}
